package com.yunhe.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.UserTier;
import com.yunhe.inter.AlertuserDao;
import com.yunhe.inter.UserDao;
import com.yunhe.util.SqlSessionFactoryUtil;

/**
 * Servlet implementation class UpdateUser
 */
@WebServlet("/UpdateUser")
public class UpdateUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       SqlSession sqlSession;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		sqlSession =  SqlSessionFactoryUtil.openSqlSession();
		UserTier ut=new UserTier();
		//用户名
		String userName=request.getParameter("userName");
		//调取方法查询该用户的信息
		AlertuserDao alertdao= sqlSession.getMapper(AlertuserDao.class);
		ut=alertdao.AlertUser(userName);
		//修改姓名
        String name=request.getParameter("name");
		name = new String(name.getBytes("iso8859-1"),"utf-8");
		ut.setRealName(name);
		//修改密码
	    String password=request.getParameter("password");
		ut.setPassWord(password);
		//修改性别
		String sex=request.getParameter("sex");
	    ut.setGender(sex);	
	    //修改用户状态
	    String state=request.getParameter("state");
	    ut.setAccountavailable(Integer.parseInt(state));
	    //修改生日
	    String birthdy=request.getParameter("birthday");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		   try {
			Date date=sdf.parse(birthdy);
			ut.setBrithDate(date);
		   } catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		   }
		   //修改手机号
		   String mobile=request.getParameter("mobile");
		   ut.setPhoneNumber(mobile);
		   alertdao.UpdateUser(ut);
		   sqlSession.commit();
		   sqlSession.close();
		   request.getRequestDispatcher("manage/manage-result.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
